<script setup>
  import { onLoad } from "@dcloudio/uni-app";
  import { onMounted, reactive } from "vue";
  import { ObjectToQueryString, ObjectToQueryStringNoEncode } from "@/utils/utils";

  const queryParams = reactive({})

  onLoad((options) => {
    Object.assign(queryParams, options)
  })

  const getWebviewSearchParams = (user) => ObjectToQueryStringNoEncode({
    'accesstoken': uni.getStorageSync("TOKEN"),
    'openid': uni.getStorageSync("OPENID"),
    'unionid': uni.getStorageSync("UNIONID"),
    'memberid': user?.memberScore?.memberId || '',
    'timestamp': new Date().getTime()
  })

  onMounted(async () => {
    Object.keys(queryParams).forEach(key => {
      queryParams[key] = decodeURIComponent(queryParams[key])
    })
    const pages = getCurrentPages()
    uni.setStorageSync('JUMP_URL', pages?.at(-1)?.fullPath)
    const { url, redirectUrl, isTabbar } = queryParams
    getApp().globalData.jumpRedirectUrl = redirectUrl || ''
    if (url.startsWith('http')) {
      await getApp().getUserLoginInfo()
      const user = await getApp().getUserInfo()
      let webUrl = `${url}${url.indexOf('?') > 0 ? '&' : '?'}${getWebviewSearchParams(user)}`
      uni.redirectTo({
        url: `/pages/jump/webview?${ObjectToQueryString({
          ...queryParams,
          'src': webUrl
        })}`
      })
      return
    }
    if (isTabbar === '1') {
      uni.switchTab({ url })
    } else {
      uni.redirectTo({ url })
    }
  })
</script>

<template>
  <view></view>
</template>

<style lang="scss" scoped>

</style>
